package com.ywb.edugui.controller;

import com.ywb.edugui.dao.SubjectDao;
import com.ywb.edugui.dao.facotry.DaoFactory;
import com.ywb.edugui.entity.Subject;
import com.ywb.edugui.entity.vo.FirstSubject;
import com.ywb.edugui.utils.ExcelUtils;
import com.ywb.edugui.utils.R;
import com.ywb.edugui.view.MainView;
import com.ywb.edugui.view.subject.SubjectListPanel;
import com.ywb.edugui.view.subject.SubjectQueryDialog;
import com.ywb.edugui.view.teacher.TeacherListPanel;

import javax.print.attribute.standard.Destination;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
 * @Author ywb
 */
public class SubjectController {
    //    阿里云中模板xlsx的url地址

    private static final String urlStr = "https://edu-ywb.oss-cn-beijing.aliyuncs.com/template0616.xlsx";
    private SubjectDao subjectDao = DaoFactory.getSubjectDao();
    private Container subjectView;

    public SubjectController(Container view){
        subjectView = view;
    }

    public R delete(Subject subject){
        if(subjectDao.delete(subject) < 1){
            return R.error();
        }
        return R.ok();
    }
    public R update(Subject subejct){
        if(subjectDao.update(subejct) < 1){
            return R.error();
        }
        return R.ok();
    }

    public R downLoadTemplate(String filepath){
        File xlsx = new File(filepath + File.separator + "template.xlsx");
        try{
            URL url = new URL(urlStr);
//            得到url连接的输入流
            BufferedInputStream bis = new BufferedInputStream(url.openStream());
//            得到被选中的文件夹路径+文件名称作为输出流
            FileOutputStream fos = new FileOutputStream(xlsx);
//            Test中测试,使用缓冲流的速度比nio通道快20倍
            byte[] buffer = new byte[1024];
            int count = 0;
            while((count = bis.read(buffer, 0, 1024)) != -1){
                fos.write(buffer, 0, count);
            }
            bis.close();
            fos.close();
            //根据filepath打开文件夹，方便用户直接打开模板文件
            Desktop.getDesktop().open(new File(filepath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.ok();
    }

    public void addSubject(File selectedFile) {
        subjectView.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        ExcelUtils.addSubjectByExcel(selectedFile);
        JOptionPane.showMessageDialog(null, "添加成功!!");
        subjectView.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        MainView.changeChildrenPanel("科目列表", new SubjectListPanel(), true);

    }

    public List<FirstSubject> getFirstSubjectList(){
        return subjectDao.firstSubjectList();
    }

    public void querySubject(String title) {
        Subject subject = subjectDao.getByTitle(title);
        JFrame parent = (JFrame) SwingUtilities.getWindowAncestor(subjectView);
        new SubjectQueryDialog(parent, true, subject).setVisible(true);
    }
}
